package cn.com.enersun.iec61850.integratedDataProcessing;


import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.openmuc.openiec61850.*;
import org.springframework.stereotype.Service;

import javax.xml.soap.Detail;
import java.io.File;
import java.util.*;

/**
 * Created by YYC on 2017/4/27.
 */
@Service
public class TableService {
    private ServerModel serverModel;

    public TableService() {

    }

    public TableService(ServerModel serverModel) {
        this.serverModel = serverModel;
    }

    public ServerModel getServerModel() {
        return serverModel;
    }

    public void setServerModel(ServerModel serverModel) {
        this.serverModel = serverModel;
    }

    public Map<String, List<TableListDetail>> getTableListDetail(String id) {
        Map<String, List<TableListDetail>> tableListDetails = new HashMap<String, List<TableListDetail>>();

        if (id.contains("DataSets")) {
            serverModel.getUrcb(id.replaceAll("DataSets", ""));
        } else if (id.contains("Reports")) {

        } else if (id.contains("DataModel")) {
            //查出逻辑节点
            ModelNode model = serverModel.findModeNodeByObjectReference(new ObjectReference(id.replaceAll("DataModel", "")));
            List<TableListDetail> tablelist = new ArrayList<TableListDetail>();
            for (ModelNode _mode : model.getChildren()) {
                if (_mode.getClass().getName().equals(FcDataObject.class.getName())) {
                    tablelist.add(new TableListDetail(
                            new ArrayList<String>() {{
                                add(_mode.getName());
                                add("");
                                add("");
                            }},
                            "Branch",
                            _mode.getReference().toString().replaceAll("\\W", ""),
                            "icon-none"));

                    List<TableListDetail> tabledetial = new ArrayList<TableListDetail>();
                    for (BasicDataAttribute bda : _mode.getBasicDataAttributes()) {
                        String temp[] = bda.toString().split(":");
                        tabledetial.add(new TableListDetail(
                                new ArrayList<String>() {{
                                    add(bda.getName());
                                    add(temp[0]);
                                    add(temp[1].trim());
                                }},
                                "leaf",
                                bda.getReference().toString().replaceAll("\\W", ""),
                                "icon-none"));
                    }
                    tableListDetails.put(_mode.getReference().toString().replaceAll("\\W", ""), tabledetial);
                }
                tableListDetails.put(model.getReference().toString().replaceAll("\\W", ""), tablelist);
            }
        }

        return tableListDetails;
    }

    public List<TableList> getTableList(String id) throws Exception {
        List<TableList> tableLists = new ArrayList<TableList>();

        if (id.contains("DataSets")) {
            serverModel.getUrcb(id.replaceAll("DataSets", ""));
        } else if (id.contains("Reports")) {
            //if(jsonMap.get(id))
        } else if (id.contains("DataModel")) {
            if (id.equals("DataModel")) {
                for (ModelNode model : serverModel.getChildList()) {
                    tableLists.add(new TableList(
                            model.getName(),
                            "icon-none",
                            "DataModel" + model.getReference().toString()));
                }
            } else {
                ModelNode model = serverModel.findModeNodeByObjectReference(new ObjectReference(id.replaceAll("DataModel", "")));
                for (ModelNode tempModel : model.getChildren()) {
                    tableLists.add(new TableList(
                            tempModel.getName(),
                            "icon-none",
                            "DataModel" + tempModel.getReference().toString().replaceAll("\\W", "")));
                }
            }
        }
        return tableLists;
    }

    public Object getTable(String id, String disType) {
        if (disType.equals("list")) {
            List<TableList> tableLists = new ArrayList<TableList>();

            if (id.contains("DataSets")) {
                if (id.equals("DataSets")) {
                    for (DataSet dataSet : serverModel.getDataSets()) {
                        tableLists.add(new TableList(
                                //去掉逻辑设备名字
                                dataSet.getReferenceStr().replaceAll(serverModel.getChildList().toString() + "/","").trim(),
                                "icon-ds",
                                "DataSets" + dataSet.getReferenceStr()));
                    }
                } else {
                    for (DataSet dataSet : serverModel.getDataSets()) {
                        if(dataSet.getReferenceStr().replaceAll("\\W","").contains(id.replaceAll("DataSets",""))) {
                            tableLists.add(new TableList(
                                    dataSet.getReferenceStr().replaceAll(serverModel.getChildList().toString() + "/","").trim(),
                                    "icon-ds",
                                    "DataSets" + dataSet.getReferenceStr()));
                        }
                    }
                }
            } else if (id.contains("Reports")) {
                if (id.equals("Reports")) {
                    for (Urcb urcb : serverModel.getUrcbs()) {
                        tableLists.add(new TableList(
                                urcb.getName(),
                                "icon-ur",
                                "Reports" + urcb.getReference().toString()));
                    }
                    for (Brcb brcb : serverModel.getBrcbs()) {
                        tableLists.add(new TableList(
                                brcb.getName(),
                                "icon-br",
                                "Reports" + brcb.getReference().toString()));
                    }
                } else {
                    for (Urcb urcb : serverModel.getUrcbs()) {
                        if(urcb.getReference().toString().replaceAll("\\W","").contains(id.replaceAll("Reports",""))) {
                            tableLists.add(new TableList(
                                    urcb.getName(),
                                    "icon-ur",
                                    "Reports" + urcb.getReference().toString()));
                        }
                    }
                    for (Brcb brcb : serverModel.getBrcbs()) {
                        if(brcb.getReference().toString().replaceAll("\\W","").contains(id.replaceAll("Reports",""))) {
                            tableLists.add(new TableList(
                                    brcb.getName(),
                                    "icon-br",
                                    "Reports" + brcb.getReference().toString()));
                        }
                    }
                }
            } else if (id.contains("DataModel")) {
                if (id.equals("DataModel")) {
                    for (ModelNode model : serverModel.getChildList()) {
                        tableLists.add(new TableList(
                                model.getName(),
                                "icon-ln",
                                "DataModel" + model.getReference().toString()));
                    }
                } else {
                    ModelNode model = serverModel.findModeNodeByObjectReference(new ObjectReference(id.replaceAll("DataModel", "")));
                    for (ModelNode tempModel : model.getChildren()) {
                        tableLists.add(new TableList(
                                tempModel.getName(),
                                "icon-ln",
                                "DataModel" + tempModel.getReference().toString().replaceAll("\\W", "")));
                    }
                }
            }
            return tableLists;

        } else if (disType.equals("table")) {
            Map<String, List<TableListDetail>> tableListDetails = new HashMap<String, List<TableListDetail>>();
            Map<String, Map<String, List<TableListDetail>>> result = new HashMap<String, Map<String, List<TableListDetail>>>();
            if (id.contains("DataSets")) {
                for(DataSet dataSet : serverModel.getDataSets()){
                    if(dataSet.toString().replaceAll("\\W","").contains(id.toString().replaceAll("\\W","").replaceAll("DataSets",""))){
                        for(FcModelNode node : dataSet.getMembers()) {
                            if(!( (node.getChildren()==null) || node.getChildren().isEmpty())){
                                List<TableListDetail> tableDetail = new ArrayList<TableListDetail>();
                                for(ModelNode _node : node.getChildList()) {
                                    tableDetail.add(new TableListDetail(
                                            new ArrayList<String>() {{
                                                add(_node.getName());
                                                add("");
                                                add(_node.toString().split(":",2)[1].trim());
                                            }},
                                            "leaf",
                                            "DataSets" + _node.getReference().toString().replaceAll("\\W", ""),
                                            "icon-da"));
                                }
                                tableListDetails.put("DataSets" + node.getReference().toString().replaceAll("\\W", ""), tableDetail);
                            }
                        }
                    }
                }
            } else if (id.contains("Reports")) {
                for (Urcb urcb : serverModel.getUrcbs()) {
                    if(urcb.getReference().toString().replaceAll("\\W","").contains(id.replaceAll("Reports",""))) {
                        BdaVisibleString dataSet = urcb.getDatSet();
                        for(DataSet _dataSet : serverModel.getDataSets()){
                            if(dataSet.toString().replaceAll("\\W","").contains(_dataSet.toString().replaceAll("\\W",""))){
                                List<TableListDetail> tablelist = new ArrayList<TableListDetail>();
                                for(FcModelNode node : _dataSet.getMembers()) {
                                    tablelist.add(new TableListDetail(
                                            new ArrayList<String>() {{
                                                add(node.toString().split("/",2)[1].split(":",2)[0]);
                                                add("");
                                                add(node.toString().split("/",2)[1].split(":",2)[1]);
                                            }},
                                            "leaf",
                                            "Reports" + node.getReference().toString().replaceAll("\\W", ""),
                                            "icon-da"));
                                }
                                tableListDetails.put("Reports" + dataSet.getReference().toString().replaceAll("\\W", ""), tablelist);
                            }
                        }
                    }
                }

                for (Brcb brcb : serverModel.getBrcbs()) {
                    if(brcb.getReference().toString().replaceAll("\\W","").contains(id.replaceAll("Reports",""))) {
                        BdaVisibleString dataSet = brcb.getDatSet();
                        for(DataSet _dataSet : serverModel.getDataSets()){
                            if(dataSet.toString().replaceAll("\\W","").contains(_dataSet.toString().replaceAll("\\W",""))){
                                List<TableListDetail> tablelist = new ArrayList<TableListDetail>();
                                for(FcModelNode node : _dataSet.getMembers()) {
                                    tablelist.add(new TableListDetail(
                                            new ArrayList<String>() {{
                                                add(node.toString().split("/",2)[1]);
                                                add("");
                                                add("");
                                            }},
                                            "Branch",
                                            "Reports" + node.getReference().toString().replaceAll("\\W", ""),
                                            "icon-do"));
                                    if(!(node.getChildren().isEmpty()) || (node.getChildren()==null)){
                                        List<TableListDetail> tableDetail = new ArrayList<TableListDetail>();
                                        for(ModelNode _node : node.getChildList()) {
                                            tableDetail.add(new TableListDetail(
                                                    new ArrayList<String>() {{
                                                        add(_node.getName());
                                                        add("");
                                                        add(_node.toString().split(":",2)[1]);
                                                    }},
                                                    "leaf",
                                                    "Reports" + _node.getReference().toString().replaceAll("\\W", ""),
                                                    "icon-da"));
                                        }
                                        tableListDetails.put("Reports" + node.getReference().toString().replaceAll("\\W", ""), tableDetail);
                                    }
                                }
                                tableListDetails.put("Reports" + dataSet.getReference().toString().replaceAll("\\W", ""), tablelist);
                            }
                        }
                    }
                }
            } else if (id.contains("DataModel")) {
                //查出逻辑节点
                ModelNode model = serverModel.findModeNodeByObjectReference(new ObjectReference(id.replaceAll("DataModel", "")));
                for (ModelNode _mode : model.getChildren()) {
                    //过滤报告控制块节点
                    if (_mode.getClass().getName().equals(FcDataObject.class.getName())) {
                        List<TableListDetail> tabledetial = new ArrayList<TableListDetail>();
                        for (BasicDataAttribute bda : _mode.getBasicDataAttributes()) {
                            String temp[] = bda.toString().split(":",2);
                            tabledetial.add(new TableListDetail(
                                    new ArrayList<String>() {{
                                        add(bda.getName());
                                        add(temp[0]);
                                        add(temp[1].trim());
                                    }},
                                    "leaf",
                                    "DataModel" + bda.getReference().toString().replaceAll("\\W", ""),
                                    "icon-da"));
                        }
                        tableListDetails.put("DataModel" + _mode.toString().replaceAll("\\W", ""), tabledetial);
                    }
                }
            }
            result.put("nodeID", tableListDetails);
            return result;
        }
        return null;
    }

    public Object getTableParent(String id, String disType) {
        if (disType.equals("table")) {
            List<TableListDetail> tableListDetails = new ArrayList<TableListDetail>();

            if (id.contains("DataSets")) {
                for(DataSet dataSet : serverModel.getDataSets()){
                    if(dataSet.toString().replaceAll("\\W","").contains(id.toString().replaceAll("\\W","").replaceAll("DataSets",""))){
                        for(FcModelNode node : dataSet.getMembers()) {
                            if((node.getChildren()==null) || (node.getChildren().isEmpty())) {
                                tableListDetails.add(new TableListDetail(
                                        new ArrayList<String>() {{
                                            add(node.toString().split("/", 2)[1].split(":", 2)[0].trim());
                                            add("");
                                            add(node.toString().split("/", 2)[1].split(":", 2)[1].trim());
                                        }},
                                        "leaf",
                                        "DataSets" + node.getReference().toString().replaceAll("\\W", ""),
                                        "icon-da"));
                            }else {
                                tableListDetails.add(new TableListDetail(
                                        new ArrayList<String>() {{
                                            add(node.toString().split("/",2)[1].trim());
                                            add("");
                                            add("");
                                        }},
                                        "Branch",
                                        "DataSets" + node.getReference().toString().replaceAll("\\W", ""),
                                        "icon-da"));
                            }
                        }
                    }
                }
            } else if (id.contains("Reports")) {
                for (Urcb urcb : serverModel.getUrcbs()) {
                    if(urcb.getReference().toString().replaceAll("\\W","").contains(id.replaceAll("Reports",""))) {
                        for (ModelNode node : urcb.getChildList()) {
                            tableListDetails.add(new TableListDetail(
                                    new ArrayList<String>() {{
                                        add(node.getName());
                                        add("");
                                        add(node.toString().split(":",2)[1]);
                                    }},
                                    "Branch",
                                    "Reports" + node.getReference().toString().replaceAll("\\W", ""),
                                    "icon-none"));
                        }
                    }
                }
                for (Brcb brcb : serverModel.getBrcbs()) {
                    if(brcb.getReference().toString().replaceAll("\\W","").contains(id.replaceAll("Reports",""))) {
                        for (ModelNode node : brcb.getChildList()) {
                            tableListDetails.add(new TableListDetail(
                                    new ArrayList<String>() {{
                                        add(node.getName());
                                        add("");
                                        add(node.toString().split(":",2)[1].trim());
                                    }},
                                    "Branch",
                                    "Reports" + node.getReference().toString().replaceAll("\\W", ""),
                                    "icon-none"));
                        }
                    }
                }
            } else if (id.contains("DataModel")) {
                //查出逻辑节点
                ModelNode model = serverModel.findModeNodeByObjectReference(new ObjectReference(id.replaceAll("DataModel", "")));
                for (ModelNode _mode : model.getChildren()) {
                    //过滤报告控制块节点
                    if (_mode.getClass().getName().equals(FcDataObject.class.getName())) {
                        tableListDetails.add(new TableListDetail(
                                new ArrayList<String>() {{
                                    add(_mode.toString().replaceAll(model.getReference().toString()+".",""));
                                    add("");
                                    add("");
                                }},
                                "Branch",
                                "DataModel" + _mode.toString().replaceAll("\\W", ""),
                                "icon-do"));
                    }
                }
            }
            return tableListDetails;
        }
        return null;
    }
}
